import axios from '../js_sdk/xtshadow-axios/axios.min'
import store from '../store'
import {
	paramsToStr,
	currentPage,
	tabbarList,
	acountList
} from './tools'
import Cache from './cache'
import {
	TOKEN,
	BACK_URL
} from '../config/cachekey'
import {
	baseURL
} from '../config/app'
import {
	getWxCode,
	toLogin,
	wxMnpLogin
} from './login'

let index = 0;

function checkParams(params) {
	if (typeof params != 'object') return params
	for (let key in params) {
		const value = params[key];
		if (value === null || value === undefined || value === "") {
			delete params[key];
		}
	}
	return params;
}



const service = axios.create({
	baseURL: baseURL,
	timeout: 10000,
	header: {
		'content-type': 'application/json'
	},

});


// request拦截器
service.interceptors.request.use(
	config => {
		config.data = checkParams(config.data)
		config.params = checkParams(config.params)
		if (config.method == 'GET') {
			config.url += paramsToStr(config.params)
		}
		config.header.token = Cache.get(TOKEN)
		return config
	},
	error => {
		// Do something with request error
		console.log(error) // for debug
		Promise.reject(error)
	}
)

const RequestCodeEnum = {
	SUCCESS: 200, //成功
	FAILED: 300, // 失败
	PARAMS_VALID_ERROR: 310, //参数校验错误
	PARAMS_TYPE_ERROR: 311, //参数类型错误
	REQUEST_METHOD_ERROR: 312, //请求方法错误
	ASSERT_ARGUMENT_ERROR: 313, //断言参数错误
	ASSERT_MYBATIS_ERROR: 314, //断言mybatis错误
	LOGIN_ACCOUNT_ERROR: 330, //登陆账号或密码错误
	LOGIN_DISABLE_ERROR: 331, //登陆账号已被禁用
	TOKEN_EMPTY: 332, // TOKEN参数为空
	TOKEN_INVALID: 333, // TOKEN参数无效
	NO_PERMISSTION: 403, //无相关权限
	REQUEST_404_ERROR: 404, //请求接口不存在
	SYSTEM_ERROR: 500 //系统错误
}
// response 拦截器
service.interceptors.response.use(
	async (response) => {
			if (response.data) {
				const {
					code,
					show,
					msg
				} = response.data;
				const {
					route,
					options
				} = currentPage()
				if (code !== 200 && show && msg) {
					uni.showToast({
						title: msg,
						icon: "none"
					})
				} else if (code == 333) {
					store.commit('LOGOUT')
					wxMnpLogin()
				}
				switch (code) {
					case RequestCodeEnum.SUCCESS:
						return Promise.resolve(response.data)
					case RequestCodeEnum.PARAMS_TYPE_ERROR:
					case RequestCodeEnum.PARAMS_VALID_ERROR:
					case RequestCodeEnum.REQUEST_METHOD_ERROR:
					case RequestCodeEnum.ASSERT_ARGUMENT_ERROR:
					case RequestCodeEnum.ASSERT_MYBATIS_ERROR:
					case RequestCodeEnum.LOGIN_ACCOUNT_ERROR:
					case RequestCodeEnum.LOGIN_DISABLE_ERROR:
					case RequestCodeEnum.NO_PERMISSTION:
					case RequestCodeEnum.FAILED:
					case RequestCodeEnum.SYSTEM_ERROR:
						uni.showToast({
							title: msg,
							icon: "none"
						})
						return Promise.reject(msg)

					case RequestCodeEnum.TOKEN_INVALID:
					case RequestCodeEnum.TOKEN_EMPTY:
						store.commit('LOGOUT')
						wxMnpLogin()
						return Promise.reject()

					default:
						return Promise.resolve(response.data)
				}
			}

			return Promise.resolve(response.data)
		},
		error => {
			// tryHideFullScreenLoading()
			console.log(error)
			console.log('err' + error) // for debug
			return Promise.reject(error)
		}
)

export default service
